package com.tencent.cloud.uikit.utils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.tencent.cloud.uikit.R;
import com.tencent.cloud.uikit.widget.FixedNoticeView;

/**
 * 提示视图工具类
 *
 * @author showxiao
 * @date 2018/9/23
 */
public class NoticeUtils {


    public static void showFixedNotice(View contentView, CharSequence text) {
        showFixedNotice(contentView, text, 0);
    }

    public static void showFixedNotice(View contentView, CharSequence text, int topOffset) {
        if (contentView == null || !(contentView.getParent() instanceof ViewGroup)) {
            return;
        }

        removeFixedNotice(contentView);

        ViewGroup parent = (ViewGroup) contentView.getParent();
        if (!(parent instanceof FrameLayout)) {
            FrameLayout frameLayout = new FrameLayout(contentView.getContext());
            frameLayout.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
            ));

            parent.removeView(contentView);
            frameLayout.addView(contentView);
            parent.addView(frameLayout);

            parent = frameLayout;
        }

        FixedNoticeView noticeView = (FixedNoticeView) LayoutInflater.from(contentView.getContext())
                .inflate(R.layout.fixed_notice, parent, false);
        noticeView.setNoticeText(text);
        ((ViewGroup.MarginLayoutParams) noticeView.getLayoutParams()).topMargin = topOffset;

        parent.addView(noticeView);
    }

    public static void removeFixedNotice(View contentView) {
        if (contentView == null || !(contentView.getParent() instanceof FrameLayout)) {
            return;
        }

        FrameLayout parent = (FrameLayout) contentView.getParent();
        View noticeView = parent.findViewById(R.id.ll_fix_notice);
        if (noticeView != null) {
            parent.removeView(noticeView);
        }
    }

}
